<?php

namespace app\admin\service;

use app\admin\model\BoxGameRemainLog;
use app\admin\model\BoxMemberRemainLog;
use app\admin\model\BoxStatisticsMemberOverviewAllLog;
use app\admin\model\BoxStatisticsMemberOverviewLog;

class BoxReportMemberOverviewService extends BaseService
{
    const platform_ios     = 1;
    const platform_android = 2;
    const platform_pc      = 3;
    public static $platform = array(
        self::platform_ios     => 'ios',
        self::platform_android => 'android',
        self::platform_pc      => 'pc',
    );

    /**
     * @Notes:数据总览
     * @Interface getList
     * @return array
     */
    public function getList()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        if (isset($param['platform']) && !empty($param['platform'])) {
            $map[] = ['platform', '=', $param['platform']];
        }


        if (empty($param['platform'])) {
            $list = (new BoxStatisticsMemberOverviewAllLog())
                ->field('id,day_time,device_num,reg_device_num,register_num,pay_amount,pay_people_num,dau,start_up_num,up_device_num,new_pay_num,new_pay_amount,wall_device_num,wall_reg_device_num,wall_register_num,wall_dau,wall_start_up_num,wall_up_device_num')
                ->where($map)
                ->order('day_time', 'desc')
                ->select()->toArray();
            //获取数据总数
            $count = (new BoxStatisticsMemberOverviewAllLog())->where($map)->count();
        } else {
            $list = (new BoxStatisticsMemberOverviewLog())
                ->field('id,day_time,platform,device_num,reg_device_num,register_num,pay_amount,pay_people_num,dau,start_up_num,up_device_num,new_pay_num,new_pay_amount,wall_device_num,wall_reg_device_num,wall_register_num,wall_dau,wall_start_up_num,wall_up_device_num,cost')
                ->where($map)
                ->order('day_time', 'desc')
                ->select()->toArray();
            //获取数据总数
            $count = (new BoxStatisticsMemberOverviewLog())->where($map)->count();
        }
        $MemberRemainLog = (new BoxMemberRemainLog())
            ->field('id,day_time,sum(account_num) as account_num,sum(d2) as d2,sum(d3) as d3,sum(d7) as d7')
            ->where($map)
            ->order('day_time', 'desc')
            ->group('day_time')
            ->select()->toArray();
        $MemberRemainLog = array_column($MemberRemainLog ?? [], null, 'day_time');


        $result = array();
        if (!empty($list)) {
            $device_num_all           = 0;
            $reg_device_num_all       = 0;
            $register_num_all         = 0;
            $device_register_rate_all = 0;
            $pay_amount_all           = 0;
            $pay_people_num_all       = 0;
            $arppu_all                = 0;
            $pay_rate_all             = 0;
            $dau_all                  = 0;
            $avg_start_up_all         = 0;
            $new_pay_num_all          = 0;
            $new_pay_amount_all       = 0;
            $new_pay_rate_all         = 0;
            $d2_all                   = 0;
            $d3_all                   = 0;
            $d7_all                   = 0;
            foreach ($list as $key => $item) {
                $cost = $item['cost'] ?? 0;
                $result[$key]['id']                   = $item['id'];
                $result[$key]['cost']                 = $cost;
                $result[$key]['average_cost']         = $item['register_num'] > 0 ? round($cost/$item['register_num'],2) : 0;
                $result[$key]['day_time']             = date("Y-m-d", strtotime($item['day_time']));//日期
                $result[$key]['platform']             = empty($param['platform']) ? '全部' : self::$platform[$param['platform']];//新增激活数
                $result[$key]['device_num']           = $item['device_num'] - $item['wall_device_num'];//新增激活数
                $result[$key]['reg_device_num']       = $item['reg_device_num'] - $item['wall_reg_device_num'];//新增设备注册数
                $result[$key]['register_num']         = $item['register_num'] - $item['wall_register_num'];//注册账号数
                $result[$key]['device_register_rate'] = empty($result[$key]['device_num']) ? "0%" : round( $result[$key]['register_num'] / $result[$key]['device_num'] * 100) . "%";//设备注册率
                $result[$key]['pay_amount']           = $item['pay_amount'];//充值金额
                $result[$key]['pay_people_num']       = $item['pay_people_num'];//账号付费数
                $result[$key]['arppu']                = empty($item['pay_people_num']) ? '0' : round($item['pay_amount'] / $item['pay_people_num'], 2) ?? '0';//账号ARPPU
                $result[$key]['pay_rate']             = empty($item['dau']) ? '0%' : round($item['pay_people_num'] / $item['dau'] * 100, 2) . "%";//账号付费率
                $result[$key]['dau']                  = $item['dau'] - $item['wall_dau'];//活跃账号数

                $result[$key]['avg_start_up']         = $item['up_device_num']-$item['wall_up_device_num'] <= 0 ? '0' : round(($item['start_up_num']-$item['wall_start_up_num']) / ($item['up_device_num']-$item['wall_up_device_num']), 2) ?? '0';//平均启动次数
                $result[$key]['new_pay_num']          = $item['new_pay_num'];//新增账号付费人数
                $result[$key]['new_pay_amount']       = $item['new_pay_amount'];//新增账号付费金额
                $result[$key]['new_pay_rate']         = empty($item['register_num']) ? '0%' : round($item['new_pay_num'] / $item['register_num'] * 100, 2) . "%";//新增账号付费率

                $day_time = $MemberRemainLog[$item['day_time']] ?? '';
                if(isset($day_time['account_num']) && $day_time['account_num'] > 0){
                    $result[$key]['d2']                   = empty($day_time['d2']) ? "0%" : round($day_time['d2'] / $day_time['account_num'] * 100) . "%";//2日留存
                    $result[$key]['d3']                   = empty($day_time['d3']) ? "0%" : round($day_time['d3'] / $day_time['account_num'] * 100). "%";//3日留存
                    $result[$key]['d7']                   = empty($day_time['d7']) ? "0%" : round($day_time['d7'] / $day_time['account_num'] * 100) . "%";//7日留存
                }else{
                    $result[$key]['d2']                   = "0%";//2日留存
                    $result[$key]['d3']                   = "0%";//3日留存
                    $result[$key]['d7']                   = "0%";//7日留存
                }


                $device_num_all           += $result[$key]['device_num'];//新增激活数
                $reg_device_num_all       += $result[$key]['reg_device_num'];//新增激活数
                $register_num_all         += $result[$key]['register_num'];//注册账号数
                $device_register_rate_all += empty($result[$key]['device_num']) ? '0' : round($result[$key]['register_num'] / $result[$key]['device_num'] * 100) ?? '0';//设备注册率
                $pay_amount_all           += $item['pay_amount'];//充值金额
                $pay_people_num_all       += $item['pay_people_num'];//账号付费数
                $arppu_all                += empty($item['pay_people_num']) ? '0' : round($item['pay_amount'] / $item['pay_people_num'], 2) ?? '0';//账号ARPPU
                $pay_rate_all             += empty($item['dau']) ? '0' : round($item['pay_people_num'] / $item['dau'] * 100, 2) ?? '0';//账号付费率
                $dau_all                  += $result[$key]['dau'];//活跃账号数
                $avg_start_up_all         += $item['up_device_num']-$item['wall_up_device_num'] <=0 ? '0' : round(($item['start_up_num']-$item['wall_start_up_num']) / ($item['up_device_num']-$item['wall_up_device_num']), 2) ?? '0';//平均启动次数
                $new_pay_num_all          += $item['new_pay_num'];//新增账号付费人数
                $new_pay_amount_all       += $item['new_pay_amount'];//新增账号付费金额
                $new_pay_rate_all         += empty($item['register_num']) ? '0' : round($item['new_pay_num'] / $item['register_num'] * 100, 2) ?? '0';//新增账号付费率

                if(isset($day_time['account_num']) && $day_time['account_num'] > 0){
                    $d2_all                   += empty($day_time['d2']) ? '0' : round($day_time['d2'] / $day_time['account_num'] * 100);//2日留存
                    $d3_all                   += empty($day_time['d3']) ? '0' : round($day_time['d3'] / $day_time['account_num'] * 100);//3日留存
                    $d7_all                   += empty($day_time['d7']) ? '0' : round($day_time['d7'] / $day_time['account_num'] * 100);//7日留存
                }

            }
            $result[$count]['day_time']             = '汇总';//日期
            $result[$count]['platform']             = '-';//新增激活数
            $result[$count]['device_num']           = $device_num_all;
            $result[$count]['reg_device_num']       = $reg_device_num_all;
            $result[$count]['register_num']         = $register_num_all;
            $result[$count]['device_register_rate'] = round($device_register_rate_all / $count) . "%";
            $result[$count]['pay_amount']           = $pay_amount_all;
            $result[$count]['pay_people_num']       = $pay_people_num_all;
            $result[$count]['arppu']                = round($arppu_all / $count, 2);
            $result[$count]['pay_rate']             = round($pay_rate_all / $count, 2) . "%";
            $result[$count]['dau']                  = $dau_all;
            $result[$count]['avg_start_up']         = round($avg_start_up_all / $count, 2);
            $result[$count]['new_pay_num']          = $new_pay_num_all;
            $result[$count]['new_pay_amount']       = $new_pay_amount_all;
            $result[$count]['new_pay_rate']         = round($new_pay_rate_all / $count, 2) . "%";
            $result[$count]['d2']                   = round($d2_all / $count) . "%";
            $result[$count]['d3']                   = round($d3_all / $count) . "%";
            $result[$count]['d7']                   = round($d7_all / $count) . "%";
        }
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $result,
            "count" => $count,
        ];
    }

    public function edit()
    {
        $params = request()->param();
        $id = $params['id'];
        $cost = $params['cost'];
        if($cost == ''){
            return message('请输入成本!',false);
        }
        $result = (new BoxStatisticsMemberOverviewLog())->update(['cost'=>$cost],['id'=>$id]);
        if(!$result) return message('成本录入失败!',false);
        return message('成本录入成功!',true);
    }

}
